/******************************************************************************
*          
This program uses the SPI ports on the JM60 chip.  It writes a specified amount of memory from
a Macronix 4Mbyte flash memory chip MX25L3235D through SPIport1
It is prompted by the push button on the JM60 to go from one code section to another.

Code layout:

ininite loop

- push a button and read the chip id to verify that communication is taking place.
     one should see C2 5E 16 written to gau8minicom
- push button to erase a sector specified by add1,ad2,add3. Flash cannot "set" bits, only clear.  
- Push a button to write the array written at write_array.  User will need to adjust lengths
- push a button and read a section of memory specified by memory location add1, add2, add3
- length of array =512
- Store in gau8Minicom array
  
****************************************************************************
Functions
  - hw_init();       MCU Initialization for speed, power and system settings
  - SPI_Init();      SPI Module Init for SPI port 1.  Connected to flash chip
  - SPI read byte();     Returns the last byte sent over the SPI port 
  - SPI write byte();   Writes a byte over the SPI port  
  - SPI read byte2();     Returns the last byte sent over the SPI port 
  - SPI write byte2();   Writes a byte over the SPI port  
Variables 
   UINT8 gau8Minicom[512];    array in which data is stored.  This chip has 4092 in RAM so this could be bigger
   UINT8   *pu8DataPointer;   used to store the location of gau8Minicom
   UINT16  u16Counter;        used as a counter
  UINT16  byte_count;         used to specify the amount of memomry read
  char    add1=0,add2=0,add3=0; specify address to read from  
                                                                          
/****************************************************************************
Header connector
       
       oo
       oo
miso1  oo  mosi1
clk1   oo  SS1   >>chip to the right                                     
       oo
       oo                                         
       oo
3.3V   oo  gnd
       
      Chip
 
CS  1  8 VCC
SO  2  7 NC                                                
WP  3  6 CLK
gnd 4  5 SI

SPI 2 port from SD card reader connection

SD port       SPI2 connection
1             CS2
2             MOSI2
5             CLK2
7             MISO2


Some commands

c7  clear all
03  00 00 00  << three bytes specifying address    read memory
06  write enable
02  00 00 00  << three bytes specifying address  then followed by data to write 
9f  read identifier   C2 5E 16
05  read status register
20  00 00 00 sector  erase, specify sector  0-1023
d8  00 00 00 block erase , specify block    0-63


Blocks 0-63   ex block 23 = 23 x 2^16 - 24 x 2^16 - 1
Each block 16 sectors, 2^12 bytes big 

sectors 0-1023    each one 4096 bytes    
ex sector 39  addresses are 39x4096 to 40x4096-1  or in hex  0x27*0x1000 to 0x28*0x1000

4meg of memory runs from 0 to 0x40 00 00   */        

      

/* Include Files */
#include <hidef.h>
#include <stdio.h>          
#include "SD.h"

/**************Function prototypes****************/



 /************* Global Variables *****************/
UINT8 gau8Minicom[512];
char write_array[350]={'b','o','b',0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x81,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x81,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x81,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00};

/************************************************************************************************/
void main(void)
{
   UINT8   *pu8DataPointer;
  UINT16  u16Counter;
  UINT16  byte_count;
  char    add1=0,add2=0,add3=0;
  char x;
  
/**************    Initializations  ***********************/  
  PTBPE_PTBPE5 = 1;                     // DEMOFLEXISJMSD board 
  PTBDD_PTBDD5 = _IN;                     // Initialize Button in 
  hw_init();                            /* MCU Initialization */
  SPI_Init();                           /* SPI Module Init */
  

  while(1)
  {
 
/***********************************************************************/
/************* Push button to complete this task: Read chip id    ****/   
    
        
       while(PTBD_PTBD5);                    /* Wait until button click */
   
       pu8DataPointer=gau8Minicom;
       SPI_SS=ENABLE;


	     SPI_Send_byte(0x9f);   //read chip id instruction
	     
       for(u16Counter=0;u16Counter<8;u16Counter++){
    	
	     *pu8DataPointer=SPI_Receive_byte();
	     pu8DataPointer++;
       }
    
    SPI_SS=DISABLE;
/************************************************************************/
/***** Push button to erase sector at address*************/   
   while(!PTBD_PTBD5); 
   while(PTBD_PTBD5);                    /* Wait until button click */
   
    add1=0;             //Specify the address 0-0x40 000 add3/add2/add1
    add2=0;
    add3=0;
    

    
    SPI_SS=ENABLE;
        SPI_Send_byte(0x06);   //enable writing
	  SPI_SS=DISABLE;
      
    SPI_SS=ENABLE;


	     SPI_Send_byte(0x020);   //erase sector instruction
	     SPI_Send_byte(add1);
	     SPI_Send_byte(add2);
	     SPI_Send_byte(add3);
	    
      
    SPI_SS=DISABLE;
   while(!PTBD_PTBD5); 
/***** Read a block of memory and store in gua8minicom array*************/   

   while(PTBD_PTBD5);                    /* Wait until button click */
   
    add1=0;             //Specify the address 0-0x40 000 add3/add2/add1
    add2=0;
    add3=0;
    
    byte_count =  512;      //  Specify the number of bytes you want to read
    
    pu8DataPointer=gau8Minicom;
    
    SPI_SS=ENABLE;


	     SPI_Send_byte(0x03);   //read chip id instruction
	     SPI_Send_byte(add1);
	     SPI_Send_byte(add2);
	     SPI_Send_byte(add3);
	     
       for(u16Counter=0;u16Counter<byte_count;u16Counter++){
    	
	     *pu8DataPointer=SPI_Receive_byte();
	     pu8DataPointer++;
       }
    
    SPI_SS=DISABLE;
    while(!PTBD_PTBD5);
  
/***** Push button to write sector at address*************/   
   while(PTBD_PTBD5);                    /* Wait until button click */
   
    add1=0;             //Specify the address 0-0x40 000 add3/add2/add1
    add2=0;
    add3=0;
    
    byte_count =  350;      //  Specify the number of bytes you want to write
    
    pu8DataPointer=write_array;
    
    SPI_SS=ENABLE;
        SPI_Send_byte(0x06);   //enable writing
	  SPI_SS=DISABLE;
      
    SPI_SS=ENABLE;


	     SPI_Send_byte(0x02);   //write chip id instruction
	     SPI_Send_byte(add1);
	     SPI_Send_byte(add2);
	     SPI_Send_byte(add3);
	    
       for(u16Counter=0;u16Counter<byte_count;u16Counter++){
    	
	     SPI_Send_byte(*pu8DataPointer);
	     pu8DataPointer++;
       }
    SPI_SS=DISABLE;
    while(!PTBD_PTBD5);

/************************************************************************/

/***** Read a block of memory and store in gua8minicom array*************/   

   while(PTBD_PTBD5);                    /* Wait until button click */
   
    add1=0;             //Specify the address 0-0x40 000 add3/add2/add1
    add2=0;
    add3=0;
    
    byte_count =  512;      //  Specify the number of bytes you want to read
    
    pu8DataPointer=gau8Minicom;
    
    SPI_SS=ENABLE;


	     SPI_Send_byte(0x03);   //read chip id instruction
	     SPI_Send_byte(add1);
	     SPI_Send_byte(add2);
	     SPI_Send_byte(add3);
	     
       for(u16Counter=0;u16Counter<byte_count;u16Counter++){
    	
	     *pu8DataPointer=SPI_Receive_byte();
	     pu8DataPointer++;
       }
    
    SPI_SS=DISABLE;
    while(!PTBD_PTBD5);

/************************************************************************/

  }
}


